package wml.switcher.view;

import wml.switcher.command.MarkCommand;
import wml.switcher.tranceiver.CommandSender;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class MarkListener {

	private CommandSender sender;

	public MarkListener(CommandSender commandSender) {
		this.sender = commandSender;
	}

	public boolean onMark(View v, MotionEvent event) {
		if (event.getPointerCount() < 2)
			return true;
		float x = event.getX(1);
		float y = event.getY(1);
		int type = 0;
		MainView view = (MainView) v;
		switch (event.getAction()) {
		case MotionEvent.ACTION_POINTER_2_DOWN:
			view.touch_start(x, y);
			type = MarkCommand.MARK_PRESS;
			Log.d("switcher", "第2个手指按下 in Mark(" + x + "," + y + ")");
			break;
		case MotionEvent.ACTION_MOVE:
			view.touch_move(x, y);
			type = MarkCommand.MARK_DRAG;
			break;
		case MotionEvent.ACTION_POINTER_2_UP:
			view.touch_up();
			type = MarkCommand.MARK_RELEASE;
			Log.d("switcher", "第2个手指松开 in Mark(" + x + "," + y + ")");
			break;
		}
		float[] pts = new float[] { x, y };
		view.getMarkMatrix().mapPoints(pts);
		this.push(type, pts[0], pts[1]);
		view.invalidate();
		return true;
	}

	private void push(int type, Float x, Float y) {
		try {
			MarkCommand mc = new MarkCommand(type, x.intValue(), y.intValue(),
					ViewController.getCurrentPosition());
			sender.sendCommand(mc);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
